﻿Imports System.Windows.Controls.Primitives


'http://andyonwpf.blogspot.com/2006/10/dropdownbuttons-in-wpf.html
'Usage:
'<Co9320:DropDownButton Content="Drop-Down">
'    <Co9320:DropDownButton.DropDown>
'        <ContextMenu>
'            <MenuItem Header="Item 1"/>
'            <MenuItem Header="Item 2"/>
'            <MenuItem Header="Item 3"/>
'        </ContextMenu>
'    </Co9320:DropDownButton.DropDown>
'</Co9320:DropDownButton>


Public Class DropDownButton
    Inherits ToggleButton

    Public Shared ReadOnly DropDownProperty As DependencyProperty = DependencyProperty.Register("DropDown", GetType(ContextMenu), GetType(DropDownButton), New UIPropertyMetadata)

    Public Sub New()
        Dim binding = New Binding("DropDown.IsOpen") With {.Source = Me}
        Me.SetBinding(IsCheckedProperty, binding)
    End Sub

    Public Property DropDown() As ContextMenu
        Get
            Return DirectCast(GetValue(DropDownProperty), ContextMenu)
        End Get
        Set(ByVal value As ContextMenu)
            SetValue(DropDownProperty, value)
        End Set
    End Property

    Protected Overrides Sub OnClick()

        If DropDown IsNot Nothing Then
            DropDown.PlacementTarget = Me
            DropDown.Placement = PlacementMode.Bottom
            DropDown.IsOpen = True
        End If
    End Sub

End Class
